package org.abettor.octopus.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 读取properties文件的工具类
 * @author abettor
 *
 */
public class PropertiesReader
{
	private static final String FILE = "./conf/conf.properties";

	private static Object synLocker = new Object();
	private static PropertiesReader instance = null;
	private Properties properties;

	/**
	 * 返回配置文件中指定属性的值
	 * @param key 属性名称
	 * @return 成功返回指定属性的值，否则返回null
	 */
	public static String get(String key)
	{
		try
		{
			PropertiesReader pr = PropertiesReader.getInstance();
			return pr.properties.getProperty(key);
		}
		catch (IOException e)
		{
			e.printStackTrace();
			return null;
		}
	}

	//	默认构造函数
	private PropertiesReader() throws IOException
	{
		InputStream is = new FileInputStream(new File(FILE));
		properties = new Properties();
		properties.load(is);
		is.close();
	}

	//	返回一个本类的singleton实例
	private static PropertiesReader getInstance() throws IOException
	{
		synchronized(synLocker)
		{
			if(instance == null)
			{
				instance = new PropertiesReader();
			}
		}

		return instance;
	}
}
